//
//  NSObject+Common.m
//  TY_MotorRemoteControl
//
//  Created by 徐伟新 on 2023/11/16.
//  Copyright © 2023 zhongdaodianzi. All rights reserved.
//

#import "NSObject+Common.h"

@implementation NSObject (Common)
- (void)showHUDWith:(NSString *)text showView:(UIView *)showView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        [self hideHUD:showView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:showView animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = text;
        hud.margin = 10.f;
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.removeFromSuperViewOnHide = YES;
        [hud hideAnimated:YES afterDelay:1.5];
    });
}

- (void)showLoadingHUD:(NSString *)text showView:(UIView *)showView{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        [self hideHUD:showView];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:showView animated:YES];
        hud.mode = MBProgressHUDModeIndeterminate;
        hud.label.text = text;
        hud.margin = 10.f;
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.label.font = [UIFont systemFontOfSize:13];
        hud.removeFromSuperViewOnHide = YES;
    });
}

- (void)delay:(CGFloat)duration action:(AlertViewCallBack)callBack {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        callBack();
    });
}

#pragma mark - Private
/**
 *  @brief  隐藏页面上的HUD
 */

- (void)hideHUD:(UIView *)view{
    
    if ([MBProgressHUD HUDForView:view]) {
            [MBProgressHUD hideHUDForView:view animated:YES];
        }
}

/// post通知
+ (void)postNoticationWithName:(NSString *)name withObject:(_Nullable id)objec {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:name object:objec];
}

@end
